package com.east.sng.controller;

import com.east.sng.entity.param.CourseParam;
import com.east.sng.entity.result.ClassroomTeachers;
import com.east.sng.entity.result.CourseFind;
import com.east.sng.service.CourseService;
import com.east.sng.service.CourseTypeService;
import com.east.sng.service.TeacherService;
import com.east.sng.utils.ValidationUtil;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author east
 * @date: 2020/12/29
 * @Description:
 **/
@Controller
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseTypeService courseTypeService;
    @Autowired
    private TeacherService teacherService;

    /**
     * 课程信息分类页面跳转
     * @param model
     * @return
     */
    @RequiresPermissions("course:list")
    @RequestMapping(value = "/listClass",method = RequestMethod.GET)
    public String listClass(Model model){
        model.addAttribute("courseTypes",this.courseTypeService.findCourseTypeColumns());
        return "course/listClass";
    }

    /**
     * 课程信息页面跳转
     * @param courseTypeId
     * @param courseName
     * @param teacherName
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @RequiresPermissions("course:list")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(Integer courseTypeId,String courseName, String teacherName,
                       @RequestParam(name="flag",defaultValue="0")Integer flag,
                       @RequestParam(name="pn",defaultValue="1")Integer pageNum,
                       @RequestParam(name = "pSize", defaultValue = "5") Integer pageSize,
                       Model model){
        Map<String, Object> map = new HashMap<>();
        map.put("courseTypeId",courseTypeId);
        map.put("courseName",courseName);
        map.put("teacherName",teacherName);
        map.put("flag",flag);
        model.addAttribute("page", PageHelper.startPage(pageNum, pageSize));
        model.addAttribute("courses",this.courseService.findCourses(map));
        return "course/list";
    }

    /**
     * 课程信息录入页面跳转
     * @param model
     * @return
     */
    @RequiresPermissions("course:add")
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String toAddCourse(Model model){
        model.addAttribute("courseTypes",this.courseTypeService.findCourseTypeColumns());
        return "course/add";
    }

    /**
     * 课程信息信息录入
     * @param course
     * @param br
     * @return
     */
    @RequiresPermissions("course:add")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String addCourse(@Valid CourseParam course, BindingResult br, RedirectAttributes attr){
        ValidationUtil.validateData(br);
        if(course.compareAge()) {
            attr.addFlashAttribute("msg","年龄范围有误");
            return "redirect:/course/add";
        }
        this.courseService.addCourse(course);
        return "success";
    }

    /**
     * 课程信息修改页面跳转
     * @param courseId
     * @param model
     * @return
     */
    @RequiresPermissions(value = {"course:list","course:add"},logical = Logical.AND)
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public String toEditCourse(Integer courseId,Model model){
        CourseFind course = this.courseService.findCourse(courseId);
        model.addAttribute("course",course);
        model.addAttribute("teachers",course!=null?this.teacherService.findTeachersColumns(course.getCourseTypeId()):null);
        model.addAttribute("classrooms",course!=null?this.courseService.findClassrooms(course.getCourseTypeId()):null);
        return "course/edit";
    }

    /**
     * 课程信息修改
     * @param course
     * @param br
     * @param attr
     * @return
     */
    @RequiresPermissions(value = {"course:list","course:add"},logical = Logical.AND)
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String editCourse(@Valid CourseParam course, BindingResult br, RedirectAttributes attr){
        ValidationUtil.validateData(br);
        if(course.compareAge()) {
            attr.addFlashAttribute("msg","年龄范围有误");
            return "redirect:/course/edit?courseId="+course.getCourseId();
        }
        this.courseService.editCourse(course);
        return "success";
    }

    /**
     * 课程信息删除
     * @param id
     * @return
     */
    @RequiresPermissions(value = {"course:list","course:add"},logical = Logical.AND)
    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @ResponseBody
    public String removeCourseType(Integer id){
        return this.courseService.removeCourse(id)?"success":"fail";
    }

    /**
     * 课程信息恢复
     * @param id
     * @return
     */
    @RequiresRoles("0")
    @RequiresPermissions(value = {"course:list","course:add"},logical = Logical.AND)
    @RequestMapping(value = "/recover",method = RequestMethod.POST)
    @ResponseBody
    public String recoverCourseType(Integer id){
        return this.courseService.recoverCourse(id)?"success":"fail";
    }

    /**
     * 课程信息批量删除
     * @param ids
     * @return
     */
    @RequiresPermissions(value = {"course:list","course:add"},logical = Logical.AND)
    @RequestMapping(value = "/removes",method = RequestMethod.POST)
    @ResponseBody
    public String removeCourses(Integer[] ids){
        return this.courseService.batchRemoveCourses(Arrays.asList(ids))?"success":"fail";
    }

    /**
     * 课程信息批量恢复
     * @param ids
     * @return
     */
    @RequiresRoles("0")
    @RequiresPermissions(value = {"course:list","course:add"},logical = Logical.AND)
    @RequestMapping(value = "/recovers",method = RequestMethod.POST)
    @ResponseBody
    public String recoverCourses(Integer[] ids){
        return this.courseService.batchRecoverCourses(Arrays.asList(ids))?"success":"fail";
    }

    /**
     * 根据专业查询价格、授课教室和教师
     * @param courseTypeId
     * @return
     */
    @RequiresPermissions("course:add")
    @RequestMapping(value = "/findcrt",method = RequestMethod.POST)
    @ResponseBody
    public ClassroomTeachers findCRT(Integer courseTypeId){
        ClassroomTeachers cts = new ClassroomTeachers(courseService.findClassrooms(courseTypeId),teacherService.findTeachersColumns(courseTypeId));
        return cts;
    }

}
